<template>
  <el-dialog
      :title="showTitle?'新增':'修改'"
      :visible.sync="saveVisible"
      width="30%"
      @closed="closeCallBack"
      center>
    <el-form :model="bookForm" ref="bookForm" :rules="bookFormRules" label-width="100px">
      <el-form-item label="书籍封面:" prop="bookImgUrl">
        <el-upload
            class="avatar-uploader"
            action="#"
            ref="fileUpload"
            :show-file-list="false"
            :http-request="uploadSectionFile"
        >
          <img v-if=" bookForm.bookImgUrl" :src="bookForm.bookImgUrl" class="avatar">
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>
      <el-form-item label="书籍名称:" prop="bookName">
        <el-input v-model="bookForm.bookName"></el-input>
      </el-form-item>
      <el-form-item label="作者:" prop="author">
        <el-input v-model="bookForm.author"></el-input>
      </el-form-item>
      <el-form-item label="ISBN:" prop="isbn">
        <el-input v-model="bookForm.isbn"></el-input>
      </el-form-item>
      <el-form-item label="出版社:" prop="press">
        <el-input v-model="bookForm.press"></el-input>
      </el-form-item>
      <el-form-item label="出版日期 :" prop="publishDate">
        <el-date-picker
            v-model="bookForm.publishDate"
            type="date"
            placeholder="选择日期">
        </el-date-picker>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
         <el-button @click="saveVisible = false">取 消</el-button>
         <el-button type="primary" @click="saveOrUpdate">提 交</el-button>
      </span>
  </el-dialog>
</template>

<script>
import {saveOrUpdateBook, uploadFile} from "@/api/book";

export default {
  name: "UserDialog",
  data() {
    return {
      showTitle: false,
      saveVisible: false,
      bookForm: {
        bookImgUrl:null
      },
      bookFormRules: {
        bookName: [
          {required: true, message: '请填写书籍名称', trigger: 'blur'}
        ],
        author: [
          {required: true, message: '请填写作者', trigger: 'blur'}
        ],
        isbn: [
          {required: true, message: '请填写ISBN', trigger: 'blur'}
        ],
        press: [
          {required: true, message: '请填写出版社', trigger: 'blur'}
        ],
        publishDate: [
          {required: true, message: '请填写出版日期', trigger: 'blur'}
        ]
      }
    }
  },
  methods: {
    /**
     * 保存、修改提交执后端
     */
    saveOrUpdate() {
      this.$refs['bookForm'].validate((valid) => {
        if (valid) {
          saveOrUpdateBook(this.bookForm).then((res) => {
            if (res.code === 0) {
              this.$message({
                type: 'success',
                message: res.msg
              });
              // 关闭弹窗
              this.saveVisible = false
              this.$emit("queryBookInfo")
            } else {
              this.$message({
                type: 'error',
                message: res.msg
              });
            }
          }).catch(error=>{
            console.error(error)
          });
        } else {
          this.$message({
            type: 'warning',
            message: '请完善信息！'
          });
          return false;
        }
      })
    },
    /**
     * 弹窗关闭回调
     */
    closeCallBack() {
      // 清空表单内容
      this.$nextTick(() => {
        this.bookForm = {}
        this.bookForm.bookImgUrl = null
      })
    },
    /**
     * 上传文件至服务器
     * @param params
     */
    uploadSectionFile(params) {
      let that = this;
      const formData = new FormData();
      formData.append("file", params.file);
      uploadFile(formData).then(res => {
        if (res.data.flag === true) {
          this.$message({
            type: 'success',
            message: res.data.msg
          });
          that.$nextTick(() => {
            that.bookForm.bookImgUrl = res.data.data;
          })
          // 清除文件
          this.clearFile();
        } else {
          this.$message({
            type: 'error',
            message: res.data.msg
          });
        }
      }).catch(error=>{
        console.error(error)
      })
    },
    /**
     * 清除文件
     */
    clearFile() {
      this.$refs.fileUpload.clearFiles()
    }
  }
}
</script>

<style scoped>
.el-date-editor {
  width: 100%;
}

.avatar-uploader {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  text-align: center;
  width: 178px;
  height: 178px;
}

.avatar-uploader:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
